<template>
	<view class="container">
		<view class="form">
			<text class="title">登录</text>
			<input class="input" v-model="username" placeholder="请输入用户名" />
			<input 
				class="input" 
				type="password" 
				v-model="password" 
				placeholder="请输入密码" 
				:focus="isPwdfocus"
			/>
			<button class="btn" type="primary" @click="handleLogin">登录</button>
			<view class="link">
				<text>没有账号？</text>
				<navigator url="/pages/login/login" open-type="navigate">去注册</navigator>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				username: '',
				password: '',
				isPwdfocus: false
			};
		},
		onNavigationBarButtonTap() {
			uni.navigateBack({
				delta: 1,
				fail: () => {
					uni.redirectTo({
						url: '/pages/index/index'
					});
				}
			});
		},
		methods: {
			handleLogin() {
				console.log("handleLogin()-->登录");
				if (!this.username || !this.password) {
					uni.showToast({
						title: '请输入完整信息',
						icon: 'none'
					});
					return;
				}
				
				console.log("handleLogin()-->登录验证"+this.username);
				if (this.username) {
					uni.showLoading({
						title: '正在登录中...'
					});
					
					setTimeout(() => {
						uni.hideLoading();
						
						uni.showToast({
							title: '登录成功',
							icon: 'success'
						});
						
						uni.switchTab({
							url: '/pages/my/my'
						});
					}, 1000);
				} else {
					uni.showToast({
						title: '密码错误，请重新输入',
						icon: 'none'
					});
					this.password = '';
					this.isPwdfocus = true;
				}
			}
		}
	};
</script>

<style>
.container {
	display: flex;
	flex-direction: column;
	align-items: center;
	justify-content: center;
	height: 100vh;
	background-color: #f5f5f5;
}
.form {
	display: flex;
	flex-direction: column;
	width: 80%;
	background-color: #fff;
	padding: 20px;
	border-radius: 8px;
	box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
.title {
	font-size: 20px;
	font-weight: bold;
	text-align: center;
	margin-bottom: 20px;
}
.input {
	margin-bottom: 15px;
	padding: 10px;
	border: 1px solid #ddd;
	border-radius: 4px;
}
.btn {
	margin-top: 10px;
	width: 100%;
	padding: 0;
}
.link {
	display: flex;
	justify-content: center;
	margin-top: 15px;
}
.link text {
	margin-right: 3px;
}
.link navigator {
	color: #007AFF;
}
</style>